﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CardLogistics.Web.Components;
using CardLogistics.Model.Purchase;
using System.Data;

namespace CardLogistics.Web.MainPage.Purchase.Contract_PayInfo
{
    public partial class AddEdit : BasePage
    {
        private static readonly CardLogistics.DAL.Purchase.Purchase_Contract_PayinfoData DAL = new DAL.Purchase.Purchase_Contract_PayinfoData();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PAYDATE.Value = DateTime.Now.ToString("yyyy-MM-dd");//付款日期
                ATTN.Value =LoggedUser.USERFULLNAME;//经办人默认为当前登录用户
                InitControl();
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void InitControl()
        {
            if (IsEdit)
            {
                string _key = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(_key))
                {
                    Purchase_Contract_Payinfo sData = DAL.GetModel(_key);
                     hf_CheckId.Value = sData.CheckId;//凭证号
                    PAYTYPE.SelectedValue = sData.PAYTYPE;//付款方式
                    SetWebControls(this.Page, sData);
                }
            }

        }
        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string result = "";
                if (IsEdit)
                {
                    result = DAL.Update(EncapsulationModel());
                }
                else
                {
                    result = DAL.Add(EncapsulationModel());
                    if (result.Split(',')[0] == "ok")
                        ChgBillNumber("付款登记");
                }

                if (result.Split(',')[0] == "ok")
                {
                    ReturnListInfo();
                }
                else if (result.Split(',')[1] == "-1")
                {
                    ShowMessageBox("单据编号已存在，不允许重复！");
                    return;
                }
                else
                {
                    ShowMessageBox("操作失败请重新操作！" + result.Split(',')[1]);
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private Purchase_Contract_Payinfo EncapsulationModel()
        {
            Purchase_Contract_Payinfo model = new Purchase_Contract_Payinfo();
            model = GetWebControls<Purchase_Contract_Payinfo>(this.Page);
            model.LASTUPDATEUSER = LoggedUser.ID;//最后修改人
            model.LASTUPDATEDATE = DateTime.Now; //最后修改时间
            model.PAYTYPE = PAYTYPE.SelectedValue;//付款方式
            model.CheckId = hf_CheckId.Value;//凭证号
            model.PAYMONEY =Convert.ToDecimal(PayMoney.Value); //付款金额
            if (IsEdit)
                model.ID = Request.QueryString["id"];
            return model;
        }
    }
}